/*
This file generates the descriptive statistics table of borrowers at different 
nonbank types, separately for corporate and consumer credit markets. 

Output:
	- Table E.4.a: Borrowers in consumer credit markets
	- Table E.4.b: Borrowers in corporate credit markets
	
*/



/////////////////////////////////////////////////////////////////////////////////
///
/// 					Consumer credit market 
///
/////////////////////////////////////////////////////////////////////////////////


use if delinquency != 1 using "${procdata}/urtepers_panel_ALL.dta", clear 

* Generate and label variables to be summarized
bysort familie_id year: egen tot_debt=total(REST_GAELD_BLB)
by familie_id year: egen nonbank_volume=total(REST_GAELD_BLB) ///
	if nonbank_lender==1
by familie_id year: gen nonbank_share=nonbank_volume/tot_debt
replace nonbank_share = 0 if nonbank_share == .

gen debt_1k = tot_debt / 1000
gen dispon_13_1k = famdispon_13 / 1000

label var debt_1k "Total unsecured debt (thsd DKK)"
label var dispon_13_1k "Disp. income (thsd DKK)"  
label var famalder "Age of eldest adult (years)"
label var famledig24mnd "Recently unemployed"
label var nonbank_share "Nonbank debt share"

// Run baseline regression to identify which observations are used in our final analysis
xtset id_borrowerlender year
local regspec  inter1-inter5, a(id_borrower#year id_lender) noconstant vce(cluster id_borrowerlender)
qui reghdfe intrate `regspec'
qui gen byte obs_sample = 1 if e(sample)
qui reghdfe lndebt `regspec'
qui gen byte obs_sample_debt = 1 if e(sample)

gegen byte in_sample = max(obs_sample obs_sample_debt), by(id_borrower year)

* Keep only borrowers at nonbanks that are used in our baseline loan-lvl lending regressions 
keep if in_sample ==  1
keep if nonbank_lender == 1

local borrower_stats debt_1k nonbank_share  dispon_13_1k  famalder  famledig24mnd   
keep `borrower_stats'   year nonbank_lender JUR_HOVED_BRA_DB07 id_borrower 

* Define nonbank type category variable 
cap drop nonbank_type
gen nonbank_type = 1 if JUR_HOVED_BRA_DB07==649230 		
replace nonbank_type = 2 if JUR_HOVED_BRA_DB07==649900	
replace nonbank_type = 3 if JUR_HOVED_BRA_DB07==649100	
replace nonbank_type = 4 if !inlist(JUR_HOVED_BRA_DB07,649230, 649900, 649100)
replace nonbank_type = 5 if mi(nonbank_type)

tab nonbank_type, gen(nonbank)

gcollapse (lastnm) nonbank1-nonbank4 `borrower_stats', by(id_borrower year) labelformat(#sourcelabel#)

** Compute summary statistics in KM sample
eststo sample_ccc: quietly estpost summarize `borrower_stats' if  nonbank1== 1, d
eststo sample_wm: quietly estpost summarize `borrower_stats' if 	nonbank2== 1, d
eststo sample_leasing: quietly estpost summarize `borrower_stats' if 	nonbank3 == 1, d
eststo sample_nontop3: quietly estpost summarize `borrower_stats' if 	nonbank4 == 1, d

* Save results 
esttab  sample_ccc sample_wm sample_leasing sample_nontop3 using ///
		"$tables/DescriptiveStats_Borrowers_Nonbanktypes.tex", ///
		prehead("\begin{tabular}{l*{4}{cccc}} \toprule") ///
		posthead("\hline \\ \multicolumn{4}{l}{\textbf{Panel A. Households}} \\\\[-1ex]") ///
		fragment ///
		mgroups("Consumer credit comp" "Wealth Managers" "Leasing companies" "Non top three", ///
		pattern(1 1 1 1) prefix(\multicolumn{@span}{c}{) ///
		suffix(}) span erepeat(\cmidrule(lr){@span})) ///
cells("mean(fmt(%6.2f) pattern(1 1 1 1)) sd(fmt(%8.2fc) pattern(1 1 1 1)) p50(fmt(%6.2f) pattern(1 1 1 1))") ///
		nomtitles nonumbers label collabels("Mean" "Std. Dev." "p50") ///
		stats(N, fmt(%12.0gc)) replace





/////////////////////////////////////////////////////////////////////////////////
///
/// 					Corporate credit market 
///
/////////////////////////////////////////////////////////////////////////////////


use if delinquency != 1 using "$procdata/URTEVIRK_FIRE_FIRM_03-18.dta", clear 

* Generate and label variables to be summarized
bysort cvrnr_borrower year: egen tot_debt=total(REST_GAELD_BLB)
drop if tot_debt<=0		//drops firms with nonpositive debt outstanding
by cvrnr_borrower year: egen nonbank_volume=total(REST_GAELD_BLB) ///
	if nonbank_lender==1
by cvrnr_borrower year: gen nonbank_share=nonbank_volume/tot_debt
replace nonbank_share = 0 if nonbank_share == .

gen debt_to_equity = (past-egul)/egul if egul>0 
gen debt_mio_dkk = tot_debt / 1000000 
gen AT_mio_dkk = GF_AT / 1000000

*label variables
label var debt_mio_dkk "Total unsecured debt (m DKK)"
label var AT_mio_dkk "Total assets (m DKK)"
label var GF_AARSV "FTE employees"
label var debt_to_equity "Debt to equity ratio"
label var borrower_age "Firm age (Years)"
label var nonbank_share "Nonbank debt share"

** Mark those observations in KM-sample
local regspec inter1-inter5, noconstant a(id_borrower#year id_lender) vce(cluster id_lender#id_borrower)
qui reghdfe intrate `regspec'
qui gen sample_reg = 1 if e(sample)
qui reghdfe lndebt `regspec'
qui gen sample_reg_debt = 1 if e(sample)

* Keep only borrowers at nonbanks that are used in our baseline loan-lvl lending regressions 
gegen byte in_sample = max(sample_reg sample_reg_debt), by(id_borrower year)

keep if nonbank_lender == 1
keep if in_sample ==  1


**** Add data on lender balance sheets
local borrower_stats AT_mio_dkk  debt_mio_dkk  nonbank_share GF_AARSV borrower_age debt_to_equity 
keep `borrower_stats'   year nonbank_lender JUR_HOVED_BRA_DB07_lender id_borrower 



* Define nonbank type category variable 
gen nonbank_type = 5 
replace nonbank_type = 1 if JUR_HOVED_BRA_DB07_lender== 649220 
replace nonbank_type = 2 if JUR_HOVED_BRA_DB07_lender== 649900 
replace nonbank_type = 3 if JUR_HOVED_BRA_DB07_lender== 649100 
replace nonbank_type = 4 if !inlist(JUR_HOVED_BRA_DB07_lender,649220, 649900, 649100)

tab nonbank_type, gen(nonbank)

** Compute summary statistics
eststo clear
local borrower_stats AT_mio_dkk  debt_mio_dkk  nonbank_share GF_AARSV borrower_age debt_to_equity 

gcollapse (lastnm) nonbank1-nonbank4 `borrower_stats', by(id_borrower year) labelformat(#sourcelabel#)

** Compute summary statistics in KM sample
eststo sample_sfc: quietly estpost summarize `borrower_stats' if nonbank1 == 1, d
eststo sample_wm: quietly estpost summarize `borrower_stats' if nonbank2 == 1, d
eststo sample_leasing: quietly estpost summarize `borrower_stats' if nonbank3 == 1, d
eststo sample_nontop3: quietly estpost summarize `borrower_stats' if nonbank4 == 1, d

esttab sample_sfc sample_wm sample_leasing sample_nontop3, ///
		cells("mean(fmt(%6.2f) pattern(1 1 1 1)) sd(fmt(%8.2fc) pattern(1 1 1 1)) p50(fmt(%6.2f) pattern(1 1 1 1))") ///
		collabels(, none) nomtitles nonumbers nolines ///
		stats(N, fmt(%12.0gc)) label

esttab sample_sfc sample_wm sample_leasing sample_nontop3 using ///
		"${tables}/DescriptiveStats_Borrowers_Nonbanktypes.tex", ///
		posthead("\\ \multicolumn{1}{l}{\textbf{Panel B. Firms}} &\multicolumn{3}{c}{Specialized finance comp}&\multicolumn{3}{c}{Wealth Managers}   &\multicolumn{3}{c}{Leasing companies} &\multicolumn{3}{c}{Non top three}   \\\cmidrule(lr){2-4}\cmidrule(lr){5-7}\cmidrule(lr){8-10}\cmidrule(lr){11-13}\\[-1ex]") ///
		fragment append nomtitles nonumbers nolines ///
		cells("mean(fmt(%6.2f) pattern(1 1 1 1)) sd(fmt(%8.2fc) pattern(1 1 1 1)) p50(fmt(%6.2f) pattern(1 1 1 1))") ///		
		postfoot("\hline\hline \end{tabular}") stats(N, fmt(%12.0gc)) label collabels(, none)


